<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;

class FinanceBank extends Base
{
    protected $table = 'finance_bank';
    protected $primaryKey = 'bank_id';
    protected $table_cloud = 'finance_bank_cloud';
    protected $table_bls = 'finance_bank_bls';
    protected $table_finance_withdraw_user = 'finance_withdraw_user';
    protected $table_finance_withdraw_company = 'finance_withdraw_company';
    public $limit_price = 3000000;  //注册个体工商户限制金额

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 创建银行卡
     * */
    public function create($data = [])
    {
        $data['created_at'] = time();
        $data['updated_at'] = time();
        $bankId = DB::table($this->table)->insertGetId($data);
        return $bankId;
    }

    /**
     * 修改银行卡
     */
    public function edit($data = [], $isSingle = false)
    {
        if ($isSingle) {
            $bankId = $data['bank_id'];
            unset($data['bank_id']);

            $data['updated_at'] = time();

            $edit = FinanceBank::find($bankId);
            foreach ($data as $key => $value) {
                $edit->$key = $value;
            }
            if ($edit->save()) {
                return $bankId;
            } else {
                return false;
            }
        } else {
            if (empty($data['where']) or empty($data['field'])) {
                return false;
            }

            $data['field']['updated_at'] = time();

            return DB::table($this->table)->where($data['where'])->update($data['field']);
        }
    }

    /**
     * 删除
     * */
    public function remove($bankId)
    {
        $flight = FinanceBank::find($bankId);

        return $flight->delete();
    }

    /**
     * 查询银行卡
     * */
    public function get($data = [], $isSingle = false)
    {
        if ($isSingle) {
            $result = DB::table($this->table)->where($data)->first();
            return self::objToArray($result);
        } else {
            $result = DB::table($this->table)->where($data)->get()->toArray();
            return self::objToArray($result);
        }
    }

    /**绑定云账户银行卡
     * @param $data
     * @return int
     */
    public function bindBankCloud($data){
        $data['created_at'] = time();
        $data['updated_at'] = time();
        return DB::table($this->table_cloud)->insertGetId($data);
    }

    /**删除云账户绑定的银行卡
     * @param $bankId
     * @return mixed
     */
    public function removeBankCloud($bankId)
    {
        return DB::table($this->table_cloud)->where('bank_id' ,$bankId)->delete();
    }

    /**查询云账户银行卡
     * @param $data
     * @return mixed
     */
    public function getBankCloud($data)
    {
        $result = DB::table($this->table_cloud)->where($data)->first();
        return $this->objToArray($result) ?? [];
    }

    /**获取用户绑定公司
     * @param $idcard_no
     * @return mixed
     */
    public function getUserCompany($idcard_no){
        $res = DB::table($this->table_finance_withdraw_user)
            ->where('idcard_no' ,$idcard_no)
            ->where('status' ,1)
            ->first();
        return $this->objToArray($res);
    }

    /**绑定用户和公司
     * @param $params
     * @return int
     */
    public function createUserCompany($params){
        return DB::table($this->table_finance_withdraw_user)
            ->insertGetId($params);
    }

    /**删除用户绑定的公司
     * @param $id
     * @return int
     */
    public function delUserCompany($id){
        return DB::table($this->table_finance_withdraw_user)
            ->where(['id'=>$id])
            ->update(['status'=>0]);
    }

    /**获取公司
     * @param $params
     * @return mixed
     */
    public function getCompany($params){
        $res = DB::table($this->table_finance_withdraw_company)
            ->where($params)
            ->get();
        return $this->objToArray($res);
    }

    public function checkCompanyname($name, $self_company_id=NULL){
        $obj = DB::table($this->table_finance_withdraw_company)
            ->where('name', '=', $name);
        if($self_company_id) $obj = $obj ->where('company_id', '<>', $self_company_id);

        return $obj -> exists();
    }


    /**创建公司
     * @param $params
     * @return int
     */
    public function createCompany($params){
        return DB::table($this->table_finance_withdraw_company)
            ->insertGetId($params);
    }

    /**编辑公司
     * @param $company_id
     * @param $updata
     * @return int
     */
    public function editCompany($company_id, $updata){
        return DB::table($this->table_finance_withdraw_company)
            ->where('company_id', $company_id)
            ->update($updata);
    }


    /**删除公司
     * @param $id
     * @return int
     */
    public function delCompany($id){
        return DB::table($this->table_finance_withdraw_company)
            ->delete($id);
    }

    /**
     * 绑定银行卡 贝灵斯
     */
    public function bindBankBls($userId, $name, $cardNo, $province, $openBank, $openName, $bankNo, $phone, $batchNo){
        $data['user_id'] = $userId;
        $data['user_name'] = $name;
        $data['idcard_no'] = $cardNo;
        $data['province'] = $province;
        $data['bank_address'] = $openBank;
        $data['owner_name'] = $openName;
        $data['bank_no'] = $bankNo;
        $data['mobile_phone'] = $phone;
        $data['batch_no'] = $batchNo;
        return DB::table($this->table_bls)->insertGetId($data);
    }

    /**
     * 银行卡列表 贝灵斯
     */
    public function bankList($userId){
        $res = DB::table($this->table_bls)
            ->where('user_id', $userId)
            ->where('is_deleted', 0)
            ->get();
        return $this->objToArray($res);
    }

    /**
     * 银行卡删除 贝灵斯
     */
    public function bankDelete($userId, $bankId=0){
        $query = DB::table($this->table_bls)
            ->where('user_id', $userId)
            ->where('is_deleted', 0);
        if(!empty($bankId)) {
            $query = $query->where('id', $bankId);
        }

        return $query->update(['is_deleted'=>1, 'deleted_at'=>date("Y-m-d H:i:s", time())]);
    }

    /**
     * 银行卡查询 贝灵斯
     */
    public function getBankInfo($data)
    {
        $data['is_deleted'] = 0;
        $result = DB::table($this->table_bls)->where($data)->first();
        return $this->objToArray($result) ?? [];
    }

}
